package cn.edu.zafu.camera;

import com.tms.app.tms.app.MyApp;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/11/25 0025.
 */

public class HeadCharUtil {

    /*******
     * 检查 头字母是否 存在于字典库
     * @param headChar
     * @return
     */
    public static boolean checkHeadChar(String headChar){
        if (headChar==null)
            return false;
        List list= getHeadCharByHeadChar(headChar);
        if (list!=null&&list.size()>0)
            return true;
        return false;
    }


    public static void updateDbKeyValue() throws DbException {
        DbManager db = x.getDb(MyApp.getDaoConfig());
        List<HeadChar> listSort = db.selector(HeadChar.class).where("status","=",-1).findAll();
    }


    public static void saveHeadCharHeadChar(String headChar){
        if (headChar==null||headChar.length()!=4){
            return;
        }
        for (int i = 0; i < headChar.length()-3; i++) {// 根据相邻临三位作为key
            String headSub=headChar.substring(i,i+3);
            HeadChar head=new HeadChar();
            head.setHeadChar(headChar);
            head.setHeadKey(headSub);
            head.setStatus(1);
            saveHeadChar(head);
        }
        for (int i = 0; i < headChar.length()-2; i++) {// 根据相邻临2位作为key
            String headSub=headChar.substring(i,i+2);
            HeadChar head=new HeadChar();
            head.setHeadChar(headChar);
            head.setHeadKey(headSub);
            head.setStatus(1);
            saveHeadChar(head);
        }
    }


    /******
     * 保存 key  value 对
     * @param headChar
     */
    public static void saveHeadChar(HeadChar headChar) {
        if (headChar==null)
            return;
        DbManager db = x.getDb(MyApp.getDaoConfig());
        try {
            if (db == null)
                return;
            if (headChar==null)
                return;
            HeadChar head =getHeadCharByHeadCharAndHeadKey(headChar.getHeadChar(),headChar.getHeadKey());
            if (head !=null){
                head.setWeight(headChar.getWeight()+1);
               // db.saveOrUpdate(headChar);
            }else {
                db.saveOrUpdate(headChar);
            }

        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    private static HeadChar getHeadCharByHeadCharAndHeadKey(String headChar, String headKey) {
        if (headChar==null||headKey==null){
            return null;
        }

        DbManager   db = x.getDb(MyApp.getDaoConfig());
       HeadChar head =null;
        try {
            if (db == null)
                return null;
            head = db.selector(HeadChar.class).where("headChar","=",headChar)
                    .and("headKey","=",headKey).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
         return head;
    }
    private static List<HeadChar> getHeadCharByHeadChar(String headChar) {
        if (headChar==null)
            return null;
        DbManager   db = x.getDb(MyApp.getDaoConfig());
        List<HeadChar> listSort = new ArrayList<>();
        try {
            if (db == null)
                return null;
            listSort = db.selector(HeadChar.class).where("headChar","=",headChar).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return listSort;
    }

    /******
     * 获取 头字母 by headKey
     * @param headKey
     * @return
     */
    private static List<HeadChar> getHeadCharByHeadKey(String headKey) {
        if (headKey==null)
            return null;
        DbManager   db = x.getDb(MyApp.getDaoConfig());
        List<HeadChar> listSort = new ArrayList<>();
        try {
            if (db == null)
                return null;
            listSort = db.selector(HeadChar.class).where("headKey","=",headKey).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return listSort;
    }

    /******
     * 根据 头key 取出 value 根据待验证key
     * @param headKey
     * @return
     */
    public static List<String> getHeadCharByKey(String headKey){
        if (headKey==null)
            return null;
        List<String > list =new ArrayList<>();
        if (checkHeadChar(headKey))// 首先显示 headkey 是否等于 headChar/ value
        {
            list.add(headKey);
            return list;
        }
        List<HeadChar> shipcodes= getHeadCharByHeadKey(headKey);
        if (shipcodes!=null&&shipcodes.size()>0)
        {
            for (HeadChar shipcode : shipcodes) {
                list.add(shipcode.getHeadChar());
            }
        }
        return list;
    }

    /*******
     * 获取头部 根据文本
     * @param text
     * @return
     */
    public static String getHeadCharByText(String text){
        if (text==null)
            return null;
        String headChar=null;
        String   regEx  =  "[a-zA-Z]";
        StringBuffer stringBuffer=new StringBuffer();
        for (int i = 0; i < text.length(); i++) {
            if (Pattern.matches(regEx,text.substring(i,i+1)))
                stringBuffer.append(text.substring(i,i+1));
        }
        headChar=stringBuffer.toString();
        if(headChar.length()>4){
            headChar=headChar.substring(0,4);
        }
        return headChar;
    }
}
